import axios from "axios";
import {MyUrl, Playlist} from "./Constant.ts";
import {useToast} from "vue-toastification";

export function hook_findPlaylistsByUserId(userId: number) {
    return axios.get(MyUrl.PLAYLIST_findPlaylistByUserId, {
        params: {
            userId: userId,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("获取歌单失败：" + error)
    })
}

export function hook_deletePlaylistById(id: number) {
    return axios.delete(MyUrl.PLAYLIST_deletePlaylistById, {
        params: {
            id: id,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("获取歌单失败：" + error)
    })
}

export function hook_deleteSongFromPlaylist(playlistId: number,songId: number) {
    return axios.delete(MyUrl.PLAYLIST_deleteSongFromPlaylist, {
        params: {
            playlistId: playlistId,
            songId: songId,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("从歌单删除歌曲失败：" + error)
    })
}



export function hook_addPlaylist(playlist:Playlist) {
    return axios.post(MyUrl.PLAYLIST_addPlaylist, JSON.stringify(playlist),{
        headers:{
            'Content-Type':'application/json'
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            useToast().success("添加成功")
            return res.data.data
        }
    }).catch(error => {
        console.error("添加歌单失败：" + error)
        useToast().error("添加歌单失败")
    })
}
export async function hook_addPlaylistSong(playlistId: number, songId: number) {
    console.log(playlistId, songId)
    return await axios.post(MyUrl.PLAYLIST_addPlaylistSong, {
        playlistId: playlistId,
        songId: songId
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("添加歌曲到歌单失败：" + error)
    })
}



//根据id拿到playlist
export function hook_findPlaylistById(id:number) {
    return axios.get(MyUrl.PLAYLIST_findPlaylistById, {
        params:{
            id: id,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("添加歌单失败：" + error)
    })
}

//根据searchKeyword拿到playlist
export function hook_findPlaylistByKeyword(keyword:string) {
    return axios.get(MyUrl.PLAYLIST_findPlaylistByKeyword, {
        params:{
            keyword: keyword,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找歌单失败：" + error)
    })
}


//根据searchKeyword 拿到playlist Page
export function hook_getPlaylistPageByKeyword(keyword:string,pageIndex:number) {
    return axios.get(MyUrl.PLAYLIST_getPlaylistPageByKeyword, {
        params:{
            keyword: keyword,
            pageIndex:pageIndex
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找歌单页面失败：" + error)
    })
}

export function hook_updatePlaylist(playlist) {
    return axios.put(MyUrl.PLAYLIST_updatePlaylist, JSON.stringify(playlist),{
        headers:{
            "Content-Type": "application/json",
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("歌单更新失败：" + error)
    })
}


export function hook_findPlaylistByIdAndUserId(playlistId,userId):Promise<boolean> {
    return axios.get(MyUrl.PLAYLIST_findPlaylistByIdAndUserId,{
        params:{
            id:playlistId,
            userId:userId,
        }
    }).then(res => {
        console.log(res.data)
        return res.data.flag
    }).catch(error => {
        console.error("查找歌单失败：" + error)
    })
}



